/**
 * This component renders a question dialog consisting of
 * question, rating and possible answers. Handles Answer clicks
 * using JSON POST to the backend.
 */
$.Controller( "QuestionAnswers", {
  init: function(){
    // uses a view to render question
	var me = this;
	Question.findOne({conversation: this.options.conversation.attr('id')}, function(q) {
		me.options.question = q;
		me.element.html("js/view/Question.ejs", q );
	});
  },
  "#skip-question click": function(el) {
	  var me = this;
	  
	  Question.findOne({conversation:me.options.conversation.attr('id')}, function(q) {
		  me.options.question = q;
		  me.element.html("js/view/Question.ejs", me.options.question );
	  });
  },
  ".answer click": function(el) {
	  var me = this,
	  	answer = el.context.attributes['id'].value.split('-'),
	  	dialog = new DialogTransfer();
	  
	  dialog.attr('questionId', me.options.question.attr('id'));
	  dialog.attr('answerId', answer[1]);
	  dialog.attr('conversationId', me.options.conversation.attr('id'));
	  dialog.attr('relevant', $('#mark-relevant').val()==="on" ? 1 : 0);

	  dialog.save(function (result) {
		  Question.findOne({conversation:me.options.conversation.attr('id')}, function(q) {
			  me.options.question = q;
			  me.element.html("js/view/Question.ejs", me.options.question );
		  });
	  });
  }
});